<?php

class Application_Model_Thing extends Application_Model_ModelAbstract
{
    const TYPE_TASK    = 2;
    const TYPE_STUDY   = 3;
    const TYPE_TRAVEL  = 4;
    const TYPE_BUY     = 5;

    protected $_id;
    protected $_type;
    protected $_priority;
    protected $_object_id;

    /**
     * @param mixed $type
     */
    public function setType($type)
    {
        $this->_type = $type;
    }

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->_type;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->_id = $id;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->_id;
    }

    /**
     * @param mixed $object_id
     */
    public function setObjectId($object_id)
    {
        $this->_object_id = $object_id;
    }

    /**
     * @return mixed
     */
    public function getObjectId()
    {
        return $this->_object_id;
    }

    /**
     * @param mixed $priority
     */
    public function setPriority($priority)
    {
        $this->_priority = $priority;
    }

    /**
     * @return mixed
     */
    public function getPriority()
    {
        return $this->_priority;
    }

    public function render()
    {
        $view = new Zend_View();
        $view->setScriptPath(APPLICATION_PATH . '/views/scripts/digest/');

        switch ($this->getType()) {
            case self::TYPE_TASK:
                $mapper = new Application_Model_IdeaMapper();
                $view->task = $mapper->getById($this->getObjectId());
                return $view->render('task.phtml');
            case self::TYPE_STUDY:
                $mapper = new Application_Model_StudyMapper(new Application_Model_DbTable_Terms(), new Application_Model_Term());
                $view->term = $mapper->getById($this->getObjectId());
                return $view->render('study.phtml');
            case self::TYPE_BUY:
                $mapper = new Application_Model_Mapper(new Application_Model_DbTable_Buy(), new Application_Model_Buy());
                $view->good = $mapper->getById($this->getObjectId());
                return $view->render('buy.phtml');
            case self::TYPE_TRAVEL:
                $mapper = new Application_Model_Mapper(new Application_Model_DbTable_Travels(), new Application_Model_Travel());
                $view->travel = $mapper->getById($this->getObjectId());
                return $view->render('travel.phtml');
        }

        return "тут будет описание того что нужно сделать";
    }

    public static function getModelByType($type, $options = null)
    {
        switch ($type) {
            case self::TYPE_TASK:
                return new Application_Model_Task($options);
            case self::TYPE_STUDY:
                return new Application_Model_Term($options);
            case self::TYPE_BUY:
                return new Application_Model_Buy($options);
        }

        return null;
    }

    public static function saveSortDataDigest($serialize)
    {
        $table = new Application_Model_DbTable_MainTask();

        foreach ($serialize as $sortKey => $data) {
            $typeId = self::_getTypeByDigestId($data);
            $objectId = $data;
            $table->update(array('priority' => $sortKey + 1), 'object_id = '. $objectId . ' AND type = ' . $typeId);
        }
    }

    public static function _getTypeByDigestId(&$cssId)
    {
        switch (true) {
            case (bool) strpos($cssId, 'buy');
                $cssId = (int) str_replace('digest-buy-id-', '', $cssId);
                return self::TYPE_BUY;
            case (bool) strpos($cssId, 'task');
                $cssId = (int) str_replace('digest-task-id-', '', $cssId);
                return self::TYPE_TASK;
            case (bool) strpos($cssId, 'travel');
                $cssId = (int) str_replace('digest-travel-id-', '', $cssId);
                return self::TYPE_TRAVEL;
            case (bool) strpos($cssId, 'term');
                $cssId = (int) str_replace('digest-term-id-', '', $cssId);
                return self::TYPE_STUDY;
        }

        return false;
    }
} 